package com.liyao.wftwnetdesk.Utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: windowtest4
 * @description:
 * @author: liu yan
 * @create: 2024-11-13 15:30
 */
public class NetworkInfoExtractor {

    public static Map<String,String> NetworkInfoExtractor(){
        try {
            Process process = Runtime.getRuntime().exec("ipconfig");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
            String line;
            Map<String,String> netMap=new HashMap<>();

            while ((line = reader.readLine())!= null) {
                if (line.contains("IPv4")) {
                    // 提取IPv4地址
                    String[] parts = line.split(":");
                    netMap.put("ipv4Address", parts[1].trim());
                }
                if (line.contains("子网掩码") || line.contains("Subnet Mask")) {
                    // 提取IPv4地址
                    String[] parts = line.split(":");
                    netMap.put("SubnetMask", parts[1].trim());
                }
                if (line.contains("默认网关") || line.contains("Default Gateway")) {
                    // 提取IPv4地址
                    String[] parts = line.split(":");
                    netMap.put("DefaultGateway", parts[1].trim());
                }
            }

            reader.close();
            process.destroy();

            if (!netMap.isEmpty()) {
                System.out.println("IPv4地址: " + netMap.get("ipv4Address"));
                System.out.println("子网掩码: " + netMap.get("SubnetMask"));
                System.out.println("默认网关: " + netMap.get("DefaultGateway"));
                return netMap;
            } else {
                System.out.println("未找到网络信息");
                return null;
            }

        } catch (
                IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
